package sape;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.XmlRpcRequest;
import org.apache.xmlrpc.client.*;
import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class SapeAPI {
    public static final String LOGIN = "shempliner";
    public static final String PASSWORD = "Webst@r";
    public static final String URL = "http://api.sape.ru/xmlrpc/?v=extended";

    public static void main(String[] args) throws MalformedURLException, XmlRpcException {
        int result = login();

        System.out.println(result);

        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL(URL));
        final XmlRpcClient client = new XmlRpcClient();
        XmlRpcTransportFactory factory = new XmlRpcSunHttpTransportFactory(client) {

            public XmlRpcTransport getTransport() {
                return new XmlRpcSunHttpTransport(client) {

                    @Override
                    protected void initHttpHeaders(XmlRpcRequest request) throws XmlRpcClientException {
                        super.initHttpHeaders(request);
                        setRequestHeader("Cookie", "");
                    }

                    @Override
                    protected Object readResponse(XmlRpcStreamRequestConfig pConfig, InputStream pStream) throws XmlRpcException {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(pStream));
                        String line;
                        try {
                            while ((line = reader.readLine()) != null) {
                                System.out.println(line);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }
                        return super.readResponse(pConfig, pStream);    //To change body of overridden methods use File | Settings | File Templates.
                    }
                };
            }
        };
        client.setConfig(config);
        client.setTransportFactory(factory);
        System.out.println(client.execute("sape.get_balance", new Object[]{}));
        System.out.println(client.execute("sape.get_user", new Object[]{}));
    }

    private static int login() throws MalformedURLException, XmlRpcException {
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL(URL));
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);
        Object[] params = new Object[]{LOGIN, PASSWORD};
        return (Integer) client.execute("sape.login", params);
    }

}
